package com.sinodata.bsm.cicp.collector.sqlserver;

import java.sql.ResultSet;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class Storage extends AbstractJDBCCollector {
    private static final Logger logger = Logger.getLogger(Storage.class);

    //    private static final int DEADLOCK_WAIT_TIME = 60;

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        try {
            String[] propMetas = getPropMeta();
            //        String serverName = this.getParamMap().get("服务器名");
            String serverName = this.getParamMap().get("ServerName");

            String sql = "sp_getstorage";
            String[] rs = this.getStrings(serverName, sql, ip);

            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            for (int i = 0; i < propMetas.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(rs[i], propMetas[i]);
            }

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception ex) {
            logger.error("failed to collect sqlserver storage ", ex);
            throw new CollectorException(ex);
        }
        return values;
    }

    private String[] getStrings(String serverName, String sql, String ip) throws CollectorException {
        String[] propValArr = new String[7];

        try {
            ResultSet rst = this.getCachedResultSet("sqlserver", ip, serverName, sql);// 数据库名不知道
            int fileGroupNum = 0;
            int dataFileNum = 0;
            int logFileNum = 0;

            float dataTotalSize = 0.0F;
            float dataUnusedSize = 0.0F;
            float logTotalSize = 0.0F;
            float logUnusedSize = 0.0F;
            if (rst != null) {
                while (rst.next()) {
                    fileGroupNum += rst.getInt(2);
                    dataFileNum += rst.getInt(3);
                    logFileNum += rst.getInt(4);
                    dataTotalSize += rst.getFloat(5);
                    dataUnusedSize += rst.getFloat(6);
                    logTotalSize += rst.getFloat(7);
                    logUnusedSize += rst.getFloat(8);
                }
            }
            propValArr[0] = "" + fileGroupNum;
            propValArr[1] = "" + dataFileNum;
            propValArr[2] = "" + formatDecimal(dataTotalSize);
            propValArr[3] = "" + formatDecimal(dataUnusedSize);
            propValArr[4] = "" + logFileNum;
            propValArr[5] = "" + formatDecimal(logTotalSize);
            propValArr[6] = "" + formatDecimal(logUnusedSize);

        } catch (Exception ex) {
            logger.error("failed to get string", ex);
            throw new CollectorException(ex);
        }

        return propValArr;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "ServerName" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "FileGroups", "DataFiles", "TotaldataFilesSpaceSize", "DataFilesFreeSpaceSize", "LogFiles", "TotalLogSpaceSize", "TotalLogFreeSpaceSize" };
    }

    private float formatDecimal(float num) {
        DecimalFormat fmt = new DecimalFormat("#.00");
        try {
            num = Float.parseFloat(fmt.format(num));
        } catch (NumberFormatException e) {
            return 0.0F;
        }
        return num;
    }

}
